package util;
// Imports
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Random;
import java.util.StringTokenizer;

public class Globals {
	// static members
	public static boolean debug;
	public static long runID = System.currentTimeMillis();
	public static Random rand = new Random(runID); // for random number generation
	public static double uncertainty; // uncertainty parameter (0.5 or 2.0)
	public static int periods;  // number of time periods for simulation
	public static int numTraits; 
	public static int numTraitValues;
	public static int numOrgTypes; // number of different organization types (of imitation strategies)
	public static int[] numOrgs;  // number of organizations to create
	public static double sensitivity;
	public static String[] imitationStrategies; // random, best, dominant
	public static int marketSize;
	public static double percentFailure;
	public static int numOrgsToReplace;
	public static int attentionLimit;
	public static double similarityThreshold;
	public static String replaceBy;
	public static int numTraitsToMutate;
	public static int maxOrgIdx;

	public static double failureCutoff = 1.28d; // cut-off for organizational failure

	public static void loadGlobals(String configFile) {
//		System.out.print(configFile);
		if (configFile.equals("parameter.txt")) {
			uncertainty = 0.5d;
			periods = 100;
			numTraits = 10;
			numTraitValues = 10;
			percentFailure = 0.0d; // 10.0d
			numOrgTypes = 1;
			numOrgs = new int[1]; numOrgs[0] = 100;
			sensitivity = 1;
			marketSize = 100;
			attentionLimit = 10;
			similarityThreshold = 0.5;
			replaceBy = "numbers";
			numTraitsToMutate = 1;
//			numOrgsToReplace = 1;
			numOrgsToReplace = (int)((marketSize * percentFailure) / 100); 
			imitationStrategies = new String[1]; imitationStrategies[0] = "dominant";
			maxOrgIdx = -1;
			debug = false;
		} else {
			Properties p = new Properties();
			try {
				p.load(new FileInputStream(configFile));
				uncertainty = Double.parseDouble(p.getProperty("uncertainty")); 
				periods = Integer.parseInt(p.getProperty("periods"));
				numTraits = Integer.parseInt(p.getProperty("numTraits"));
				numTraitValues = Integer.parseInt(p.getProperty("numTraitValues"));
				percentFailure = Double.parseDouble(p.getProperty("percentFailure"));
				sensitivity = Double.parseDouble(p.getProperty("sensitivity"));
				attentionLimit = Integer.parseInt(p.getProperty("attentionLimit"));
				similarityThreshold = Double.parseDouble(p.getProperty("similarityThreshold"));
				replaceBy = p.getProperty("replaceBy");
				numTraitsToMutate = Integer.parseInt(p.getProperty("numTraitsToMutate"));
				if (replaceBy.equals("numbers") || replaceBy.equals("strategy")) {
					// do nothing "numbers" or "strategy" are valid options
				} else {
					System.err.println("Replacement (replaceBy) must be either 'numbers' or 'strategy'.  Please verify and try again.");
					System.exit(0);
				}
				StringTokenizer stno = new StringTokenizer(p.getProperty("numOrgs"), ",");
				StringTokenizer sti = new StringTokenizer(p.getProperty("imitationStrategies"), ",");
				if (stno.countTokens() != sti.countTokens()) {
					System.err.println("Number of organizations (numOrgs), number of sensitivities (sensitivities) and number of imitation strategis in the config file do not match.  Please verify and try again");
					System.exit(0);
				} else {
					numOrgTypes = stno.countTokens();
					numOrgs = new int[numOrgTypes];
					if (Globals.debug) {
						System.out.println("num org types = " + numOrgTypes);
					}
					imitationStrategies = new String[numOrgTypes];
					int i = 0;
					while (stno.hasMoreTokens()) { 
						numOrgs[i] = Integer.parseInt(stno.nextToken());
						if (numOrgs[i] < 1) { System.err.println("Number of organizations must be positive.  Try again.  "); System.exit(0); }
						marketSize += numOrgs[i]; // add to marketsize so we don't have to recalculate
						i++; 
					}
					i = 0; 
					while (sti.hasMoreTokens()) {
						imitationStrategies[i] = sti.nextToken();
						i++;
					}
					numOrgsToReplace = (int)((marketSize * percentFailure) / 100); 
				} // END if stno.countTokens == sts.countTokens
				maxOrgIdx = -1;
//				debug = Boolean.parseBoolean("debug");
				if (Globals.debug) {
					System.out.println(debug );
				}
			} catch (Exception e) {
				System.err.println(e.getMessage());
				e.printStackTrace();
			} // END try..catch
		} // END if configFile == parameter.txt
		
	} // END loadGlobals()
}